package fag.editor;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.event.EventListenerList;

public class ShapeChooser extends JPanel implements ActionListener {

	private static final long serialVersionUID = 6859423556700716972L;
	
	private final static int DEFAULT_WIDTH = 160;
	private final static int DEFAULT_HEIGHT = 75;
	
	private JToggleButton tbtRectangle = new JToggleButton(new ImageIcon(getClass().getResource("/imgs/rectangle.png"), "RectangleIcon"));
	private JToggleButton tbtCircle = new JToggleButton(new ImageIcon(getClass().getResource("/imgs/circle.png"), "CircleIcon"));
	private JToggleButton tbtPolygon = new JToggleButton(new ImageIcon(getClass().getResource("/imgs/polygone.png"), "PolygoneIcon"));
	
	protected final static String ACTION_CHANGED_SHAPE = "Shape : Changed";

	private final EventListenerList listeners = new EventListenerList();

	private String currentShape = "none";
	
	
	public ShapeChooser() {
		
		setBorder(BorderFactory.createTitledBorder("Shape"));
		
		add(tbtRectangle);
		add(tbtCircle);
		add(tbtPolygon);

		tbtRectangle.setPreferredSize(new Dimension(40,40));
		tbtCircle.setPreferredSize(new Dimension(40,40));
		tbtPolygon.setPreferredSize(new Dimension(40,40));
		
		tbtRectangle.addActionListener(this);
		tbtCircle.addActionListener(this);
		tbtPolygon.addActionListener(this);
		
		repaint();
	}
	
	public String getCurrentShape() {
		
		return currentShape;
	}
	
	public void resetSelection() {
		
		tbtRectangle.setSelected(false);
		tbtCircle.setSelected(false);
		tbtPolygon.setSelected(false);
	}
	
	public void setClickAction(boolean b) {
		
		tbtRectangle.setEnabled(b);
		tbtCircle.setEnabled(b);
		tbtPolygon.setEnabled(b);
	}
	
	@Override
	public Dimension preferredSize() {
		
		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}
	
	public void addActionListener(ActionListener listener) {
		
		listeners.add(ActionListener.class,listener);
	}
	
	public void removeActionListener(ActionListener listener) {
		
		listeners.remove(ActionListener.class, listener);
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == tbtRectangle) {
			
			tbtRectangle.setSelected(true);
			tbtCircle.setSelected(false);
			tbtPolygon.setSelected(false);
			currentShape = "Rectangle";
			currentShapeChanged();
		}
		
		if(e.getSource() == tbtCircle) {
					
			tbtRectangle.setSelected(false);
			tbtCircle.setSelected(true);
			tbtPolygon.setSelected(false);
			currentShape = "Circle";
			currentShapeChanged();
		}
		
		if(e.getSource() == tbtPolygon) {
			
			tbtRectangle.setSelected(false);
			tbtCircle.setSelected(false);
			tbtPolygon.setSelected(true);
			currentShape = "Polygon";
			currentShapeChanged();
		}
	}
	
	protected void currentShapeChanged() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_CHANGED_SHAPE));
	}
}
